package org.apache.lucene.ocean.merge;

import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.lucene.ocean.name.NameService.CellNodes;
import org.apache.lucene.ocean.name.NameService.NameCellNodes;
import org.apache.lucene.ocean.name.NameService.Node;

/**
 * Each new cell nodes needs to be cloned and .
 *
 */
public abstract class MergeClient {
  private AtomicReference<NameCellNodes> nameCellNodesRef = new AtomicReference<NameCellNodes>();
  private static Random random = new Random(System.currentTimeMillis());
  
  public void updateCellNodes(NameCellNodes nameCellNodes) {
    nameCellNodesRef.set(nameCellNodes);
  }
  
  public NameCellNodes getNameCellNodes() {
    return nameCellNodesRef.get();
  }
  
  /**
   * For a given set of cellnodes, make a distributed query
   * @param cellNodes
   * @return
   */
  public MergeResults search() throws Exception {
    NameCellNodes nameCellNodes = getNameCellNodes();
    for (CellNodes cellNodes : nameCellNodes.cellNodesArray) {
      Node node = getRandomNode(cellNodes.nodes);
      
    }
  }
  
  private Node getRandomNode(Node[] nodes) {
    int i = random.nextInt(nodes.length);
    return nodes[i];
  }
  
  public void update(CellNodes cellNodes) {
    
  }
}
